﻿{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "银行征信结合用户年龄、用户贷款金额、用户婚姻状况勾稽比对\n",
    "* 征信白户： 除了多头数据外，其余数据为0， 加上年龄\n",
    "* 征信婚姻状况和用户自填的婚姻状况不一致比对，风险分析\n",
    "* 办卡集中度：回溯时间点：预审创建时间，近6个月、12个月、24个月\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"./xym_utils\")\n",
    "from xym_utils import utilDBConnect\n",
    "from xym_utils.db_config import mysqlDB,impalaDB\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib as mlt\n",
    "from matplotlib import pyplot as plt\n",
    "import scorecardpy as sc\n",
    "import json\n",
    "import time\n",
    "import  datetime\n",
    "import re\n",
    "pd.set_option('max_columns',1000)\n",
    "pd.set_option('max_row',300)\n",
    "pd.set_option('display.float_format', lambda x: '%.5f' % x)\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus']=False #用来正常显示符号"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "def get_month(begin, end, format=\"%Y-%m-%d\"):\n",
    "    begin = datetime.datetime.strptime(begin,format)\n",
    "    end = datetime.datetime.strptime(end,format)\n",
    "    begin_year,end_year=begin.year,end.year\n",
    "    begin_month,end_month=begin.month,end.month\n",
    "    if begin_year==end_year:\n",
    "        months=end_month-begin_month\n",
    "    else:\n",
    "        months=(end_year-begin_year)*12+end_month-begin_month\n",
    "    return months"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "'未婚'"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_marriage(x):\n",
    "    \"\"\"\n",
    "    获取婚姻信息\n",
    "    :param x:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    pattern_marriage = re.compile(r'(已婚|未婚|离异)')  #\n",
    "    result = pattern_marriage.findall(x)\n",
    "    result_marriage = result[0] if len(result)>0 else \"\"\n",
    "    return result_marriage\n",
    "\n",
    "x = \"\"\"\n",
    "未婚     近1.1\n",
    "\"\"\"\n",
    "get_marriage(x)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "(0, 0)"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_org_search_cnt(x, type=\"3\"):\n",
    "    \"\"\"\n",
    "    近期查询次数，type=1为近1个月机构查询\n",
    "    eg x = ' 已婚  近1.4  近3.6+1 近3.6  近24.11+1 近24.4+1'\n",
    "    :param x:\n",
    "    :param type:1/3/6\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    # 近期查询次数\n",
    "    pattern_str = \"(近[ ]*)(\"+str(type)+\")([ ]*\\.[ ]*)(\\d+)(?![\\d+\\+])\" # 不以什么结尾 eg 近24.1正确，近24.11+1错\n",
    "\n",
    "    pattern = re.compile(r''+pattern_str)  #\n",
    "    result = pattern.findall(x)\n",
    "    org_cnt_list = [0]\n",
    "    # mth_list = [] # 近*个月\n",
    "    for i in result:\n",
    "        org_cnt_list.append(int(i[3]))\n",
    "        # mth_list.append(i[1])\n",
    "    org_cnt_max = max(org_cnt_list)\n",
    "    org_cnt_sum = sum(org_cnt_list)\n",
    "    return org_cnt_max,org_cnt_sum\n",
    "\n",
    "x = ' 已婚  近1.4  近3.6+1 近3.6  近24.11+1 近24.4+1'\n",
    "get_org_search_cnt(x, type=\"24\")"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "(14, 8, 6)"
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_cum_overed_info(x):\n",
    "    \"\"\"\n",
    "    累计逾期信息汇总\n",
    "    \"\"\"\n",
    "    pattern = re.compile(r'(累逾[ ]*)(\\d+)([ ]*\\+*[ ]*)(\\d*)')  #\n",
    "    result1 = pattern.findall(x)\n",
    "    # print(len(result1))\n",
    "    # result1\n",
    "    overed_cnt_cum_list = [0]  # 累计逾期次数\n",
    "    overed_cnt_conti_list = [0]  # 连续逾期次数\n",
    "    overed_cnt_cum_max = 0  # 累计逾期次数最大\n",
    "    overed_cnt_conti_max = 0  # 连续逾期次数最大\n",
    "    overed_cnt_all = 0  # 逾期总次数\n",
    "    overed_cnt_conti_max = 0\n",
    "    for i in result1:\n",
    "        if i[1] != \"\":\n",
    "            overed_cnt_cum_list.append(int(i[1]))\n",
    "\n",
    "        if i[3] != \"\":\n",
    "            overed_cnt_conti_list.append(int(i[3]))\n",
    "\n",
    "    overed_cnt_all = np.sum(overed_cnt_cum_list)\n",
    "    overed_cnt_cum_max = np.max(overed_cnt_cum_list)\n",
    "    overed_cnt_conti_max = np.max(overed_cnt_conti_list)\n",
    "\n",
    "    return overed_cnt_all, overed_cnt_cum_max, overed_cnt_conti_max\n",
    "\n",
    "x = \"\"\"\n",
    "未婚     卡授2000/1865    近1.3\n",
    "23.9-24.9额1000个消/1000/12/0\n",
    "1贷清17.6额97600累逾8+2/6335/20.8\n",
    "23.1-99.12额2600个消/2351/9/275\n",
    "卡1     2000/1738\n",
    "1卡销额1万累逾1+1/772/20.12\n",
    "1卡销额0累逾5+6/3644/21.9\n",
    "近3.5\n",
    "\"\"\"\n",
    "get_cum_overed_info(x)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "(10, 2, 4)"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def get_latest_mth_overinfo_dtype(x, type=12):\n",
    "    \"\"\"\n",
    "    近24/12个月累计逾期次数 和连续逾期天数\n",
    "    :param x: x = \"近24.4+2累逾11+2/3276  近 24 . 1 + 1  近 24  1 + 1 近 2 . 2+1  近24 . 3 + 1  近24 1+1\"\n",
    "    :param type: 12/24\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    x_type = '(近[ ]*)('+str(type)+')([ ]*\\.*[ ]*)(\\d+)([ ]*\\+[ ]*)(\\d+)'\n",
    "    # print(x_type)\n",
    "    pattern = re.compile(r''+x_type)  #\n",
    "    result1 = pattern.findall(x)\n",
    "    # print(len(result1))\n",
    "\n",
    "    overed_cnt_list = [0]\n",
    "    overed_days_list = [0]\n",
    "    overed_cnt_sum = 0  # 所有累计逾期次数\n",
    "    overed_days_max = 0 # 逾期最大天数（这里1指的是1个月内，2为逾期超过一个月，小于两个月）\n",
    "    overed_cnt_max = 0 # 逾期最大次数\n",
    "    for i in result1:\n",
    "        if i[3] != \"\":\n",
    "            overed_cnt_list.append(int(i[3])) # 逾期次数\n",
    "        if i[5] != \"\":\n",
    "            overed_days_list.append(int(i[5])) # 逾期天数\n",
    "\n",
    "    overed_cnt_sum = sum(overed_cnt_list)\n",
    "    overed_days_max = max(overed_days_list)\n",
    "    overed_cnt_max = max(overed_cnt_list)\n",
    "\n",
    "    return overed_cnt_sum,overed_days_max,overed_cnt_max\n",
    "\n",
    "x = \"\"\"近24.4+2累逾11+2/3276  近 24 . 1 + 1  近 24  1 + 1 近 2 . 2+1  近24 . 3 + 1  近24 1+1\"\"\"\n",
    "get_latest_mth_overinfo_dtype(x, type=24)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 150000.0, 1)\n",
      "(4, 800000.0, 1)\n"
     ]
    }
   ],
   "source": [
    "def judge_guarantee(x):\n",
    "    # 对外担保\n",
    "    # 对外担保笔数、对外担保金额\n",
    "    # x = \"\"\"\n",
    "    #   4对外担保其中19.8-21.8对外担保10万   19.8-21.8对外担保30万   19.8-21.8对外担保20万   19.8-21.8对外担保20万\n",
    "    # \"\"\"\n",
    "\n",
    "    dwdb_flag = 0\n",
    "    dwdb_cnt = 0 # 对外担保笔数\n",
    "    dwdb_amt_list = [0] # 对外担保金额\n",
    "\n",
    "    # case 1\n",
    "    pattern_dwdb= re.compile(r'(担保)(\\d+)(笔)(额)(\\d+)(万{0,1})')  #\n",
    "    result1 = pattern_dwdb.findall(x)\n",
    "    # print(result1)\n",
    "\n",
    "\n",
    "    for i in result1:\n",
    "        dwdb_flag = 1\n",
    "        if i[1]!=\"\":\n",
    "            dwdb_cnt += int(i[1])\n",
    "\n",
    "        dwdb_amt = 0\n",
    "        if i[4] !=\"\":\n",
    "            dwdb_amt = float(i[4])\n",
    "            if i[5]!= \"\":\n",
    "                dwdb_amt = dwdb_amt*1e4\n",
    "        dwdb_amt_list.append(dwdb_amt)\n",
    "\n",
    "    # case2\n",
    "    # x=\"\"\"\n",
    "    #       4对外担保其中19.8-21.8对外担保10万   19.8-21.8对外担保30万   19.8-21.8对外担保20万   19.8-21.8对外担保20万\n",
    "    # \"\"\"\n",
    "    pattern_dwdb= re.compile(r'(对外担保)(\\d+)(万{0,1})')  #\n",
    "    result2 = pattern_dwdb.findall(x)\n",
    "    # print(result2)\n",
    "\n",
    "\n",
    "    for i in result2:\n",
    "        dwdb_flag = 1\n",
    "        dwdb_cnt += 1\n",
    "        dwdb_amt = 0\n",
    "        if i[1] != \"\":\n",
    "            dwdb_amt = float(i[1])\n",
    "            if i[2] != \"\":\n",
    "                 dwdb_amt *= 1e4\n",
    "        dwdb_amt_list.append(dwdb_amt)\n",
    "\n",
    "    return dwdb_cnt,sum(dwdb_amt_list),dwdb_flag\n",
    "\n",
    "x = \"\"\"\n",
    "已婚    担保2笔额15万\n",
    "\"\"\"\n",
    "print(judge_guarantee(x))\n",
    "x=\"\"\"\n",
    "4对外担保其中19.8-21.8对外担保10万   19.8-21.8对外担保30万   19.8-21.8对外担保20万   19.8-21.8对外担保20万\n",
    "\"\"\"\n",
    "print(judge_guarantee(x))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "data": {
      "text/plain": "(1, 13118000.0, 35622.0)"
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def mortgage_resolve(x):\n",
    "    # 房贷解析\n",
    "    # x = \"\"\"\n",
    "    # 09.4-24.4额26万房贷/月还2124\n",
    "    # 09.4-24.4额50万房贷/月还1万\n",
    "    # 13.11-33.10额38万房贷/月还2512近24.1+1累逾13+1/2512\n",
    "    # 19.9-49.e939万房贷/月还2128\n",
    "    # 19.4-29.4额28万房贷/月还3647\n",
    "    # 1贷清额19万房贷近24.2+1累逾22+1/2047/19.4\n",
    "    # 18.4-38.4额194万房贷/月还13765近24.1+1\n",
    "    # 15.6-30.6额178000房贷/月还1446累逾1+1/1446\n",
    "    # 近24.1+1\n",
    "    # \"\"\"\n",
    "\n",
    "    # ([0-9\\-\\.]+|[贷清]*)(额|e)(\\d+)(万*)(房贷)(/*)([月还]*)(\\d*)(万*)(近*)(\\d*)(\\.*)(\\d*)(\\+*)(\\d*)\n",
    "    pattern = re.compile(r'([0-9\\-\\.]+|[贷清]*)(额|e)(\\d+)(万*)(房贷)(/*)([月还]*)(\\d*)(万*)')  #\n",
    "    result1 = pattern.findall(x)\n",
    "\n",
    "    # 房贷额度\n",
    "    mortgage_flag = 0 # 是否有房贷\n",
    "    mortgage_amt_tot = 0  # 房贷总额\n",
    "    # mortgage_payment_mthly_list = []  # 房贷月还列表\n",
    "    mortgage_payment_mthly_tol = 0  # 房贷月还总额\n",
    "\n",
    "\n",
    "    mortgage_flag =  1 if re.search(\"房贷\",x) else 0\n",
    "\n",
    "    for i in result1:\n",
    "        # 房贷额度\n",
    "        mortgage_amt = float(i[2])\n",
    "        if i[3] != \"\":\n",
    "            mortgage_amt = mortgage_amt * 1e4\n",
    "\n",
    "        mortgage_amt_tot +=  mortgage_amt\n",
    "\n",
    "        # 房贷月还\n",
    "        mortgage_repy_mly = float(i[7]) if i[7] != \"\" else 0\n",
    "        if i[8] != \"\":\n",
    "            mortgage_repy_mly = mortgage_repy_mly * 1e4\n",
    "        # print(mortgage_repy_mly)\n",
    "        # mortgage_payment_mthly_list.append(mortgage_repy_mly)\n",
    "        # 月还款总额\n",
    "        mortgage_payment_mthly_tol +=  mortgage_repy_mly\n",
    "\n",
    "    return mortgage_flag,mortgage_amt_tot,mortgage_payment_mthly_tol\n",
    "\n",
    "\n",
    "x = \"\"\"\n",
    "09.4-24.4额26万房贷/月还2124\n",
    "09.4-24.4额50万房贷/月还1万\n",
    "13.11-33.10额38万房贷/月还2512近24.1+1累逾13+1/2512\n",
    "19.9-49.e939万房贷/月还2128\n",
    "19.4-29.4额28万房贷/月还3647\n",
    "1贷清额19万房贷近24.2+1累逾22+1/2047/19.4\n",
    "18.4-38.4额194万房贷/月还13765近24.1+1\n",
    "15.6-30.6额178000房贷/月还1446累逾1+1/1446\n",
    "近24.1+1\n",
    "\"\"\"\n",
    "mortgage_resolve(x)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "outputs": [],
   "source": [
    "# 车贷\n",
    "def get_car_loan(x):\n",
    "    \"\"\"\n",
    "    :param x:\n",
    "    :return:\n",
    "    car_loan_cnt\t    车贷查得次数\n",
    "    car_loan_exists_jq\t车贷结清次数\n",
    "    car_edu_max\t        车贷最大额度\n",
    "    car_edu_sum\t        车贷累计额度\n",
    "    car_mth_repay_sum\t车贷累计月还金额\n",
    "    car_loan_overed_cnt_max_m24\t 车贷近24个月最大逾期次数\n",
    "    car_loan_overed_cnt_sum_m24\t 车贷近24个月累计逾期次数\n",
    "    car_loan_overed_days_max_m24 车贷近24个月最大逾期天数(1: M1, 2:M2,...)\n",
    "    car_loan_cum_overed_cnt_sum\t 车贷累计逾期次数和\n",
    "    car_loan_cum_overed_cnt_max\t 车贷累计逾期次数最大值\n",
    "    car_loan_cum_overed_days_max 车贷累计逾期天数最大值\n",
    "    \"\"\"\n",
    "    # x = \"\"\"\n",
    "    # 18.9-21.9额82000车贷/月还2758\n",
    "    # 18.12-21.12额134868车贷/月还4245\n",
    "    # 17.7额26320/车贷已结清\n",
    "    # 17.7额26320/车贷已结清，近24.1+1，累逾1+1\n",
    "    # 17.7额6580/车贷已结清，近24.2+1\n",
    "    # 车贷17.11-20.6额192500/已结清，累逾2+1\n",
    "    # 17.7          额46740车贷结清  近243+1/1948  累逾3+1/1948\n",
    "    # 19.6车贷结清   近24  4+1/2097  累逾 4+1/2097\n",
    "    # 1  22.8-27.12额122300车贷/110974/53/2601月还近24.5+1/2601\n",
    "    #  23.5-24.5额7805个消/6565/10/69223.5-26.5额150500车贷/143425/4963月还\n",
    "    # 2  21.12-26.12额10万车贷/74536/42/2074月还/近24.6+1/2077  近24.1+1/4231\n",
    "    # 3  22.9-25.9额47000车贷/月还1673近24.7+1/1678  近24.7+1/4231\n",
    "    # 4   1贷清额322000车贷近24.8+1累逾5+1/9883/19.6\n",
    "    # 5  1贷清额27720车贷近24.9+1累逾6+1/1001/19.10\n",
    "    # 6   20.2-22.2额19万车贷/月还10502近24.10+1\n",
    "    # 7  1贷清18.6额67000车贷近24.11+1/1944/20.4\n",
    "    # 8  1贷清额49975车贷近24.12+1/1660/20.9\n",
    "    # 9  1贷清额74000车贷近24.13+1累逾7+1/1862/19.9\n",
    "    # 10   1贷清额74000车贷近24.14+1累逾8+1/1862/19.9\n",
    "    # 11 18.10-21.10额413000车贷/月还/12668近24.15+2/25336\n",
    "    # 12   1贷清17.8额35000车贷近24.16+1累逾9+1/1631/19.9\n",
    "    # 13  19.8-22.8额71968车贷/月还0 近24.17+1\n",
    "    # 14 19.10-22.10额490224车贷/月还15000近24.18+1\n",
    "    # 15 1贷清额4万车贷近24.19+1/1901/20.1\n",
    "    # 16   18.4-21.4额53325车贷/月还1951近24.20+1累逾10+1/1958\n",
    "    # 17 20.10-23.10额87500车贷/月还3033近24.21+1\n",
    "    # 18 18.6-21.6额9万车贷/月还2704近24.22+1累逾11+1/2658\n",
    "    # 19 1贷清额60800车贷近24.23+1累逾12+1/2025/21.1   近24.1+1/4231    23.5-24.5额7805个消/6565/10/69223.5-26.5额150500车贷/143425/4963月还\n",
    "    # 20 23.3-26.3额65000车贷/62012/2193月还近24.24+1/2194  20.11-50.11额84万房贷/811861/4635月还\n",
    "    # 近24.1+1/4231    23.5-24.5额7805个消/6565/10/69223.5-26.5额150500车贷/143425/4963月还\n",
    "    # \"\"\"\n",
    "\n",
    "    car_loan_cnt = 0  # 车贷数量，0不存在\n",
    "    edu_list = [0] # 额度\n",
    "    car_repay_list = [0] # 月还\n",
    "    car_loan_exists_jq = 0 # 含车贷未结清 0,含车贷，存在结清1，不含车贷null\n",
    "    car_edu_max = 0\n",
    "    car_edu_sum = 0\n",
    "    car_mth_repay_sum = 0\n",
    "\n",
    "    tmp = re.findall(\"车贷\",x)\n",
    "    car_loan_cnt =  len(re.findall(\"车贷\",x)) if  tmp else 0\n",
    "\n",
    "    # 已结清\n",
    "    pattern_jq = re.compile(r'(车贷)([\\d\\-\\.]*额*\\d*\\/*)([已结清|结清]+)')  #\n",
    "    result_jq = pattern_jq.findall(x)\n",
    "\n",
    "    car_loan_exists_jq = len(result_jq)\n",
    "\n",
    "    # 车贷求额度有两种格式\n",
    "    # case1 含月还\n",
    "    pattern_edu = re.compile(r'([0-9\\-\\. ]+)(额|e)(\\d+)(万*)(/*)(车贷)(/*)([月还]*)(\\d*)(万*)')  #\n",
    "    result_edu= pattern_edu.findall(x)\n",
    "\n",
    "    for i in result_edu:\n",
    "        if i[2] !=\"\":\n",
    "            edu = float(i[2])\n",
    "            if i[3] != \"\":\n",
    "                edu = edu * 1e4\n",
    "            edu_list.append(edu)\n",
    "        # 月还\n",
    "        if i[8] !=\"\":\n",
    "            mth_repay = float(i[8])\n",
    "            if i[9] != \"\" and re.search(\"万\",i[9]):\n",
    "                mth_repay = mth_repay * 1e4\n",
    "            car_repay_list.append(mth_repay)\n",
    "\n",
    "    pattern_edu_2 = re.compile(r'(车贷)([0-9\\-\\. ]+)(额|e)(\\d+)(万*)')  #\n",
    "    result_edu_2 = pattern_edu_2.findall(x)\n",
    "\n",
    "    for i in result_edu_2:\n",
    "        # 额度\n",
    "        if i[3] !=\"\":\n",
    "            edu = float(i[3])\n",
    "        if i[4] != \"\":\n",
    "            edu = edu * 1e4\n",
    "        edu_list.append(edu)\n",
    "\n",
    "    car_edu_max = max(edu_list)\n",
    "    car_edu_sum = sum(edu_list)\n",
    "    car_mth_repay_sum = sum(car_repay_list)\n",
    "\n",
    "    car_loan_overed_cnt_m24_list = [0]\n",
    "    car_loan_overed_days_max_m24_list = [0]\n",
    "    car_loan_cum_overed_cnt_list = [0]\n",
    "    car_loan_cum_overed_days_list = [0]\n",
    "    # 累逾\n",
    "    # pattern_jq = re.compile(r'(车贷)([\\d\\-\\.]*额*\\d*\\/*)([已结清|结清]+)')  #\n",
    "    # result_jq = pattern_jq.findall(x)\n",
    "    pattern_ly = re.compile(r'(车贷)[ \\./近\\d\\-已结清额近\\+,，月还]*(累逾)[ \\.]*(\\d+)(\\+)(\\d+)')\n",
    "    result_ly = pattern_ly.findall(x)\n",
    "\n",
    "    for i in result_ly:\n",
    "        car_loan_cum_overed_cnt_list.append(int(i[2]))\n",
    "        car_loan_cum_overed_days_list.append(int(i[4]))\n",
    "\n",
    "    # 近24个月逾期情况\n",
    "    pattern_l24 = re.compile(r'(车贷)[ \\./累逾\\d\\-已结清额\\+,，月还]*(近24)[ \\.]*(\\d+)(\\+)(\\d+)')\n",
    "    result_l24 = pattern_l24.findall(x)\n",
    "    for i in result_l24:\n",
    "        car_loan_overed_cnt_m24_list.append(int(i[2]))\n",
    "        car_loan_overed_days_max_m24_list.append(int(i[4]))\n",
    "\n",
    "    car_loan_overed_cnt_max_m24 = np.max(car_loan_overed_cnt_m24_list)\n",
    "    car_loan_overed_cnt_sum_m24 = np.sum(car_loan_overed_cnt_m24_list)\n",
    "    car_loan_overed_days_max_m24 = np.max(car_loan_overed_days_max_m24_list)\n",
    "    car_loan_cum_overed_cnt_sum = np.sum(car_loan_cum_overed_cnt_list)\n",
    "    car_loan_cum_overed_cnt_max = np.max(car_loan_cum_overed_cnt_list)\n",
    "    car_loan_cum_overed_days_max = np.max(car_loan_cum_overed_days_list)\n",
    "\n",
    "    return car_loan_cnt,car_loan_exists_jq,car_edu_max,car_edu_sum,car_mth_repay_sum\\\n",
    "        ,car_loan_overed_cnt_max_m24,car_loan_overed_cnt_sum_m24,car_loan_overed_days_max_m24\\\n",
    "        ,car_loan_cum_overed_cnt_sum,car_loan_cum_overed_cnt_max,car_loan_cum_overed_days_max\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "x = \"\"\"\n",
    "    18.9-21.9额82000车贷/月还2758\n",
    "    18.12-21.12额134868车贷/月还4245\n",
    "    17.7额26320/车贷已结清\n",
    "    17.7额26320/车贷已结清，近24.1+1，累逾1+1\n",
    "    17.7额6580/车贷已结清，近24.2+1\n",
    "    车贷17.11-20.6额192500/已结清，累逾2+1\n",
    "    17.7          额46740车贷结清  近243+1/1948  累逾3+1/1948\n",
    "    19.6车贷结清   近24  4+1/2097  累逾 4+1/2097\n",
    "    1  22.8-27.12额122300车贷/110974/53/2601月还近24.5+1/2601\n",
    "     23.5-24.5额7805个消/6565/10/69223.5-26.5额150500车贷/143425/4963月还\n",
    "    2  21.12-26.12额10万车贷/74536/42/2074月还/近24.6+1/2077  近24.1+1/4231\n",
    "    3  22.9-25.9额47000车贷/月还1673近24.7+1/1678  近24.7+1/4231\n",
    "    4   1贷清额322000车贷近24.8+1累逾5+1/9883/19.6\n",
    "    5  1贷清额27720车贷近24.9+1累逾6+1/1001/19.10\n",
    "    6   20.2-22.2额19万车贷/月还10502近24.10+1\n",
    "    7  1贷清18.6额67000车贷近24.11+1/1944/20.4\n",
    "    8  1贷清额49975车贷近24.12+1/1660/20.9\n",
    "    9  1贷清额74000车贷近24.13+1累逾7+1/1862/19.9\n",
    "    10   1贷清额74000车贷近24.14+1累逾8+1/1862/19.9\n",
    "    11 18.10-21.10额413000车贷/月还/12668近24.15+2/25336\n",
    "    12   1贷清17.8额35000车贷近24.16+1累逾9+1/1631/19.9\n",
    "    13  19.8-22.8额71968车贷/月还0 近24.17+1\n",
    "    14 19.10-22.10额490224车贷/月还15000近24.18+1\n",
    "    15 1贷清额4万车贷近24.19+1/1901/20.1\n",
    "    16   18.4-21.4额53325车贷/月还1951近24.20+1累逾10+1/1958\n",
    "    17 20.10-23.10额87500车贷/月还3033近24.21+1\n",
    "    18 18.6-21.6额9万车贷/月还2704近24.22+1累逾11+1/2658\n",
    "    19 1贷清额60800车贷近24.23+1累逾12+1/2025/21.1   近24.1+1/4231    23.5-24.5额7805个消/6565/10/69223.5-26.5额150500车贷/143425/4963月还\n",
    "    20 23.3-26.3额65000车贷/62012/2193月还近24.24+1/2194  20.11-50.11额84万房贷/811861/4635月还\n",
    "    近24.1+1/4231    23.5-24.5额7805个消/6565/10/69223.5-26.5额150500车贷/143425/4963月还\n",
    "    \"\"\"\n",
    "get_car_loan(x)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "outputs": [],
   "source": [
    "# 个经\n",
    "def get_gj_resolve(x, credit_time =\"\"):\n",
    "    # x = \"\"\"\n",
    "    #     19.12-20.12额10万个经/季还0\n",
    "    #     18.5-21.5额10万个经/季还/0\n",
    "    #     19.10-21.8额207667个经/月还18060\n",
    "    #     19.11-20.11额101000个经/月还560\n",
    "    #     20.9-21.9额37000个经/月还0\n",
    "    #     20.9-21.3额13万个经/月还191\n",
    "    #     20.10-21.4额6万个经/月还0\n",
    "    #     20.3-21.3额15万个经/一次性0\n",
    "    #     20.7-21.2额55000个经/月还9763\n",
    "    #      20.7-21.2额55000个经    1贷清额5000个消累逾3+3/1587/18.8\n",
    "    # \"\"\"\n",
    "\n",
    "    pattern_gj= re.compile(r'(个经)')  #\n",
    "    result = pattern_gj.findall(x)\n",
    "\n",
    "    gj_cnt_tot = len(result)\n",
    "    gj_edu_list = [0]\n",
    "    gj_repay_list = [0]\n",
    "    gj_edu_sum = 0\n",
    "    gj_repay_sum = 0\n",
    "    gj_edu_max = 0\n",
    "\n",
    "    pattern_gj= re.compile(r'([0-9\\-\\.]+)额(\\d+)(万{0,1})个经(/*)([月还|季还|一次性还]*)(/*)(\\d*)(万*)')  #\n",
    "    result = pattern_gj.findall(x)\n",
    "\n",
    "\n",
    "    mth_diff_list = []\n",
    "    for i in result:\n",
    "        # 额度\n",
    "        if i[1]!=\"\":\n",
    "            gj_edu = np.float(i[1])\n",
    "            if i[2]!=\"\":\n",
    "                gj_edu = gj_edu*1e4\n",
    "            gj_edu_list.append(gj_edu)\n",
    "        # 还款信息\n",
    "        if i[6]!=\"\":\n",
    "            gj_repay = np.float(i[6])\n",
    "            if i[7]!=\"\":\n",
    "                gj_repay = gj_repay*1e4\n",
    "            gj_repay_list.append(gj_repay)\n",
    "\n",
    "        # 借款时间\n",
    "        if credit_time !=\"\":\n",
    "                time_res = re.compile(r'(\\d+) *\\. *(\\d+) *-*').findall(i[0])\n",
    "                if len(time_res)>0:\n",
    "                    try:\n",
    "                        # 获取贷款年月\n",
    "                        loan_year = \"20\"+time_res[0][0]\n",
    "                        loan_month = time_res[0][1]\n",
    "                        loan_month = \"0\"+loan_month if len(loan_month)==1 else loan_month\n",
    "                        loan_time = loan_year+\"-\"+loan_month\n",
    "                        # print(loan_time)\n",
    "                        mth_diff_list.append(get_month(loan_time,credit_time[:7],\"%Y-%m\" ))\n",
    "                    except Exception as  e:\n",
    "                        print(\"【mortgage_resolve ERROR】: \", e)\n",
    "\n",
    "    gj_edu_sum = np.sum(gj_edu_list)\n",
    "    gj_repay_sum = np.sum(gj_repay_list)\n",
    "    gj_edu_max = np.max(gj_edu_list)\n",
    "\n",
    "    # 近6个月，近12个月，近24个月\n",
    "    mth_diff_series = pd.Series(mth_diff_list)\n",
    "    # print(mth_diff_list)\n",
    "    loan_cnt_m6 = (mth_diff_series<=6).sum()\n",
    "    loan_cnt_m12 = (mth_diff_series<=12).sum()\n",
    "    loan_cnt_m24 = (mth_diff_series<=24).sum()\n",
    "\n",
    "    return gj_cnt_tot,gj_edu_sum,gj_edu_max,gj_repay_sum, loan_cnt_m6, loan_cnt_m12,loan_cnt_m24\n",
    "\n",
    "\n",
    "def get_gx_resolve(x,credit_time=\"\"):\n",
    "    # x = \"\"\"\n",
    "    #     1贷清额5000个消累逾3+3/1587/18.8\n",
    "    #     20.10-21.10额4000个消/月还4001\n",
    "    #     18.8-21.7额470个消/月还432\n",
    "    #      19.11-22.11额51000个消/月还1589\n",
    "    #      20.11-21.2额2000个消/月还0\n",
    "    #      19.1-21.1额1150个消/月还63\n",
    "    #      20.1-21.1额5万个消/不定期0\n",
    "    #      1贷清额1710个消累逾1+1/1088/19.8\n",
    "    # \"\"\"\n",
    "\n",
    "\n",
    "    pattern_gx= re.compile(r'(个消)')  #\n",
    "    result = pattern_gx.findall(x)\n",
    "\n",
    "    gx_cnt_tot = len(result)\n",
    "\n",
    "    gx_edu_list = [0]\n",
    "    gx_repay_list = [0]\n",
    "    gx_edu_sum = 0\n",
    "    gx_repay_sum = 0\n",
    "    gx_edu_max = 0\n",
    "\n",
    "    pattern_gx= re.compile(r'([0-9\\-\\.]*)(额)(\\d+)(万{0,1})(个消)(/*)([月还|季还|一次性还]*)(/*)(\\d*)(万*)')  #\n",
    "    result = pattern_gx.findall(x)\n",
    "\n",
    "    mth_diff_list = []\n",
    "    for i in result:\n",
    "        # 额度\n",
    "        if i[2]!=\"\":\n",
    "            gx_edu = np.float(i[2])\n",
    "            if i[3]!=\"\":\n",
    "                gx_edu = gx_edu*1e4\n",
    "            gx_edu_list.append(gx_edu)\n",
    "        # 还款\n",
    "        if i[8]!=\"\":\n",
    "            gx_repay = np.float(i[8])\n",
    "            if i[9]!=\"\":\n",
    "                gx_repay = gx_repay*1e4\n",
    "            gx_repay_list.append(gx_repay)\n",
    "\n",
    "        #\n",
    "        if credit_time !=\"\":\n",
    "                time_res = re.compile(r'(\\d+) *\\. *(\\d+) *-*').findall(i[0])\n",
    "                if len(time_res)>0:\n",
    "                    try:\n",
    "                        # 获取贷款年月\n",
    "                        loan_year = \"20\"+time_res[0][0]\n",
    "                        loan_month = time_res[0][1]\n",
    "                        loan_month = \"0\"+loan_month if len(loan_month)==1 else loan_month\n",
    "                        loan_time = loan_year+\"-\"+loan_month\n",
    "                        mth_diff_list.append(get_month(loan_time,credit_time[:7],\"%Y-%m\" ))\n",
    "                        # print(loan_time)\n",
    "                    except Exception as  e:\n",
    "                        print(\"【mortgage_resolve ERROR】: \", e)\n",
    "\n",
    "    gx_edu_sum = np.sum(gx_edu_list)\n",
    "    gx_repay_sum = np.sum(gx_repay_list)\n",
    "    gx_edu_max = np.max(gx_edu_list)\n",
    "\n",
    "    # 近6个月，近12个月，近24个月\n",
    "    mth_diff_series = pd.Series(mth_diff_list)\n",
    "    # print(mth_diff_list)\n",
    "    loan_cnt_m6 = (mth_diff_series<=6).sum()\n",
    "    loan_cnt_m12 = (mth_diff_series<=12).sum()\n",
    "    loan_cnt_m24 = (mth_diff_series<=24).sum()\n",
    "\n",
    "    return gx_cnt_tot, gx_edu_sum, gx_edu_max,gx_repay_sum ,loan_cnt_m6,loan_cnt_m12,loan_cnt_m24\n",
    "\n",
    "\n",
    "\n",
    "## 农户\n",
    "def  get_nh_resolve(x,credit_time=\"\"):\n",
    "    # x = \"\"\"已婚    21.6-24.6额16000农户/16000/9季还\n",
    "    # 1贷清额23万农户累逾3+1/506/21.12\n",
    "    # 21.9-23.9额49000农户/49000/3半年还\n",
    "    #  21.9-23.9额49000农户/49000/3半年还\n",
    "    #  20.2-23.2额5万农户/5万/2/0季还\n",
    "    # 20.10-23.10额4万农户/4万/3半年还\n",
    "    # 1贷清18.10额3万农户累逾1+1/555/19.10\n",
    "    # \"\"\"\n",
    "\n",
    "    pattern = re.compile(\n",
    "        r'([0-9\\-\\.]*)(额)(\\d+)(万{0,1})(农户{0,1})(/*)(\\d*)([累逾]*)(\\d*\\+*\\d*)')  # 贷款起始时间， 贷款额度， 待还款额度， 剩余还款期数， 月还金额(一般为0）很多没有这个字段  还款方式\n",
    "    result = pattern.findall(x)\n",
    "\n",
    "    nh_cnt_tot = len(result)\n",
    "    nh_edu_list = [0]\n",
    "    nh_edu_max = 0\n",
    "    nh_edu_sum = 0\n",
    "    mth_diff_list = []\n",
    "\n",
    "    for i in result:\n",
    "        if i[2]!=\"\":\n",
    "            nh_edu = np.float(i[2])\n",
    "            if i[3] != \"\":\n",
    "                nh_edu = nh_edu * 1e4\n",
    "            nh_edu_list.append(nh_edu)\n",
    "\n",
    "        # 贷款时间范围\n",
    "        if credit_time !=\"\":\n",
    "            time_res = re.compile(r'(\\d+) *\\. *(\\d+) *-*').findall(i[0])\n",
    "            if len(time_res)>0:\n",
    "                try:\n",
    "                    # 获取贷款年月\n",
    "                    loan_year = \"20\"+time_res[0][0]\n",
    "                    loan_month = time_res[0][1]\n",
    "                    loan_month = \"0\"+loan_month if len(loan_month)==1 else loan_month\n",
    "                    loan_time = loan_year+\"-\"+loan_month\n",
    "                    mth_diff_list.append(get_month(loan_time,credit_time[:7],\"%Y-%m\" ))\n",
    "                    # print(loan_time)\n",
    "                except Exception as  e:\n",
    "                    print(\"【mortgage_resolve ERROR】: \", e)\n",
    "\n",
    "\n",
    "    nh_edu_max = np.max(nh_edu_list)\n",
    "    nh_edu_sum = np.sum(nh_edu_list)\n",
    "    # 近6个月，近12个月，近24个月\n",
    "    mth_diff_series = pd.Series(mth_diff_list)\n",
    "    # print(mth_diff_list)\n",
    "    loan_cnt_m6 = (mth_diff_series<=6).sum()\n",
    "    loan_cnt_m12 = (mth_diff_series<=12).sum()\n",
    "    loan_cnt_m24 = (mth_diff_series<=24).sum()\n",
    "    return nh_cnt_tot, nh_edu_max,nh_edu_sum,loan_cnt_m6,loan_cnt_m12,loan_cnt_m24\n",
    "\n",
    "\n",
    "## 其他贷款\n",
    "def get_qt_resolve(x,credit_time=\"\"):\n",
    "    # x = \"\"\"\n",
    "    # 19.8-22.8额20万其他/月还/6187\n",
    "    # 20.6-21.12额990其他/月还58\n",
    "    # 20.6-21.12额990其他/月还58\n",
    "    # 20.11-21.11额5万其他/月还0\n",
    "    # 19.9-24.9额0其他/其他18\n",
    "    # 20.1-21.1额3万其他/不定期/0\n",
    "    #    22.8-23.8额20万其他/20万/0不定期\n",
    "    # \"\"\"\n",
    "\n",
    "    #额度解析\n",
    "    pattern = re.compile(r'([0-9\\-\\.]+)(额)(\\d+)(万*)(其他)')  # 贷款起始时间， 贷款额度， 待还款额度， 剩余还款期数， 月还金额(一般为0）很多没有这个字段  还款方式\n",
    "    result = pattern.findall(x)\n",
    "    qt_cnt_tot= len(result)\n",
    "\n",
    "    qt_edu_list = [0]\n",
    "    for i in result:\n",
    "        # 额度\n",
    "        if i[2]!=\"\":\n",
    "            qt_edu = np.float(i[2])\n",
    "            if i[3] != \"\":\n",
    "                qt_edu = qt_edu * 1e4\n",
    "            qt_edu_list.append(qt_edu)\n",
    "\n",
    "    pattern_repay = re.compile(r'([0-9\\-\\.]+)(额)(\\d+)(万*)(其他)(/*)(月还)(/*)(\\d+)(万*)')  # 贷款起始时间， 贷款额度， 待还款额度， 剩余还款期数， 月还金额(一般为0）很多没有这个字段  还款方式\n",
    "    result_repay = pattern_repay.findall(x)\n",
    "\n",
    "    qt_repay_list = [0]\n",
    "\n",
    "    for i in result_repay:\n",
    "        # 月还\n",
    "        if i[8]!=\"\":\n",
    "            qt_repay = np.float(i[8])\n",
    "            if i[9] != \"\":\n",
    "                qt_repay = qt_repay * 1e4\n",
    "            qt_repay_list.append(qt_repay)\n",
    "\n",
    "    qt_edu_sum = np.sum(qt_edu_list)\n",
    "    qt_edu_max = np.max(qt_edu_list)\n",
    "    qt_repay_sum = np.sum(qt_repay_list)\n",
    "\n",
    "    mth_diff_list = []\n",
    "    for i in result:\n",
    "        # 贷款时间范围\n",
    "        if credit_time !=\"\":\n",
    "            time_res = re.compile(r'(\\d+) *\\. *(\\d+) *-*').findall(i[0])\n",
    "            if len(time_res)>0:\n",
    "                try:\n",
    "                    # 获取贷款年月\n",
    "                    loan_year = \"20\"+time_res[0][0]\n",
    "                    loan_month = time_res[0][1]\n",
    "                    loan_month = \"0\"+loan_month if len(loan_month)==1 else loan_month\n",
    "                    loan_time = loan_year+\"-\"+loan_month\n",
    "                    mth_diff_list.append(get_month(loan_time,credit_time[:7],\"%Y-%m\" ))\n",
    "                    # print(loan_time)\n",
    "                except Exception as  e:\n",
    "                    print(\"【mortgage_resolve ERROR】: \", e)\n",
    "\n",
    "\n",
    "    # 近6个月，近12个月，近24个月\n",
    "    mth_diff_series = pd.Series(mth_diff_list)\n",
    "    # print(mth_diff_list)\n",
    "    loan_cnt_m6 = (mth_diff_series<=6).sum()\n",
    "    loan_cnt_m12 = (mth_diff_series<=12).sum()\n",
    "    loan_cnt_m24 = (mth_diff_series<=24).sum()\n",
    "\n",
    "    return qt_cnt_tot, qt_edu_sum, qt_edu_max,qt_repay_sum,loan_cnt_m6,loan_cnt_m12,loan_cnt_m24\n",
    "\n",
    "\n",
    "def  get_card_resolve(x,credit_time=\"\"):\n",
    "    # x = \"\"\"\n",
    "    # 卡1    15000 / 13712\n",
    "    # 卡2    3 万 / 1.5 万\n",
    "    #    24000 / 0 / 0大额\n",
    "    # 卡6    24000/0/0大额近24.1+1累逾2+1/4287\n",
    "    # 卡2    23.3-26.5额10239/7/2443大额\n",
    "    # 卡5    22.10-22.10额0/0/2044大额近24.1+1累逾2+1/2919\n",
    "    # 卡5   22.8-23.8额10万/62455/1330大额近24.1+1/1326\n",
    "    # 卡4   21.1-21.4额150000/80000/2843大额\n",
    "    # 卡2    23.3-26.5额10239/7/2443大额\n",
    "    # 卡1   2000/1840近24.3+1累逾12+1/1063\n",
    "    # 卡2   4000/3680累逾4+1/195\n",
    "    # 卡2  20.8-23.8额3889/2/2210大额\n",
    "    # 卡1  12000/451累逾1+1/196\n",
    "    # 卡1    6000/5993近24.1+1/782\n",
    "    # 卡4  19.3-25.12额103111/29/6964大额\n",
    "    # 卡5  20.8-20.10额35000/35000/671大额\n",
    "    # 1卡销额6000累逾1+1/53/19.5\n",
    "    # 1卡销额24000累逾11+2/24999/21.3\n",
    "    # 卡6    20.7额7596/15200/425（大额）\n",
    "    #  卡5   20.7额34488/69000/1931（ 大额）\n",
    "    # \"\"\"\n",
    "\n",
    "    # case 1: 卡1  15000/13712   授信额度/已使用额度\n",
    "    edu_list = [0]\n",
    "    edu_used_list = [0]\n",
    "    edu_used_ratio = [0]\n",
    "    card_edu_max = 0\n",
    "    card_edu_mean = 0\n",
    "    card_cnt = 0\n",
    "    edu_used_ratio_mean = 0\n",
    "    edu_used_ratio_max = 0\n",
    "\n",
    "    pattern_card = re.compile(r'(卡)[ ]*(\\d+)')\n",
    "    result_card = pattern_card.findall(x)\n",
    "    card_cnt = len(result_card)\n",
    "\n",
    "\n",
    "    pattern_card_1 = re.compile(r'(卡)[ ]*(\\d+)[ ]*([\\d\\.]+)[ ]*(万*)[ ]*([ ]*/[ ]*)([\\d\\.]+)[ ]*(万*)')  #\n",
    "    result = pattern_card_1.findall(x)\n",
    "\n",
    "    for i in result:\n",
    "        edu = 0\n",
    "        if i[2] != \"\":\n",
    "            edu = float(i[2])\n",
    "            if i[3]!= \"\":\n",
    "                edu = edu*1e4\n",
    "        edu_list.append(edu)\n",
    "\n",
    "        used = 0\n",
    "        if i[5] != \"\":\n",
    "            used = float(i[5])\n",
    "            if i[6]!=\"\":\n",
    "                used *= 1e4\n",
    "        edu_used_list.append(used)\n",
    "\n",
    "\n",
    "    card_edu_max = max(edu_list)\n",
    "    card_edu_mean = np.mean(edu_list)\n",
    "\n",
    "\n",
    "    # edu_sum  = sum(edu_list)\n",
    "    # edu_used_weight_ratio = np.sum([j/i*(i/edu_sum)  if i>0 else 0 for i,j in zip(edu_list, edu_used_list)]) # 加权平均额度使用率\n",
    "    # 总额度使用率\n",
    "    edu_used_sum = np.sum(edu_used_list)\n",
    "    edu_sum = np.sum(edu_list)\n",
    "    card_edu_used_sum_ratio  =  np.round(edu_used_sum/edu_sum if edu_sum>0 else 0,4)\n",
    "\n",
    "\n",
    "    # case2 大额 额度/剩余额度/月还\n",
    "    # 24000/0/0大额\n",
    "    # 额0/0/2044大额\n",
    "    # 卡5    22.10-22.10额0/0/2044大额近24.1+1累逾2+1/2919\n",
    "    # 卡5   22.8-23.8额10万/62455/1330大额近24.1+1/1326\n",
    "    #  卡6    20.7额7596/15200/425（大额）\n",
    "    pattern_large_edu_1 = re.compile(r\"大额\")\n",
    "    result_large_edu_1  = pattern_large_edu_1.findall(x)\n",
    "    card_large_edu_cnt = len(result_large_edu_1)\n",
    "\n",
    "\n",
    "    pattern_large_edu = re.compile(r'([0-9\\.\\-]*)([额 ]*)(\\d+)(万*)([ ]*/[ ]*)(\\d+)(万*)([ ]*/[ ]*)(\\d+)(万*)[ ]*([ （]*[ ]*大额[ ）]*)')\n",
    "    result_large_edu = pattern_large_edu.findall(x)\n",
    "\n",
    "    large_edu_list = [0] # 大额额度列表\n",
    "    large_edu_left_list = [0] #大额额度剩余额度\n",
    "    large_edu_mth_repay_list = [0] # 大额月还列表\n",
    "    mth_diff_list = [] # 和申请时间月份差\n",
    "    for i in result_large_edu:\n",
    "        # 额度列表\n",
    "        edu = 0\n",
    "        if i[2] != \"\":\n",
    "            edu = float(i[2])\n",
    "            if i[3]!=\"\":\n",
    "                edu *= 1e4\n",
    "        large_edu_list.append(edu)\n",
    "        # 剩余额度列表\n",
    "        left_edu = 0\n",
    "        if i[5] != \"\":\n",
    "            left_edu = float(i[5])\n",
    "            if i[6]!=\"\":\n",
    "                left_edu *= 1e4\n",
    "        large_edu_left_list.append(left_edu)\n",
    "        #月还\n",
    "        repay_edu = 0\n",
    "        if i[8] != \"\":\n",
    "            repay_edu = float(i[8])\n",
    "            if i[9]!=\"\":\n",
    "                repay_edu *= 1e4\n",
    "        large_edu_mth_repay_list.append(repay_edu)\n",
    "\n",
    "        # 贷款时间范围\n",
    "        if credit_time !=\"\":\n",
    "            time_res = re.compile(r'(\\d+) *\\. *(\\d+) *-*').findall(i[0])\n",
    "            if len(time_res)>0:\n",
    "                try:\n",
    "                    # 获取贷款年月\n",
    "                    loan_year = \"20\"+time_res[0][0]\n",
    "                    loan_month = time_res[0][1]\n",
    "                    loan_month = \"0\"+loan_month if len(loan_month)==1 else loan_month\n",
    "                    loan_time = loan_year+\"-\"+loan_month\n",
    "                    mth_diff_list.append(get_month(loan_time,credit_time[:7],\"%Y-%m\" ))\n",
    "                    # print(loan_time)\n",
    "                except Exception as  e:\n",
    "                    print(\"【mortgage_resolve ERROR】: \", e)\n",
    "\n",
    "    card_large_edu_mth_repay_sum = sum(large_edu_mth_repay_list) # 累计月还\n",
    "    #加权额度使用率\n",
    "    large_edu_sum = np.sum(large_edu_list)\n",
    "    card_large_edu_used_sum_ratio = np.round(np.sum([(i-j)/i*(i/large_edu_sum)  if i>0 else 0 for i,j in zip(large_edu_list, large_edu_left_list)]),4) # 加权平均额度使用率\n",
    "    # 近6个月，近12个月，近24个月\n",
    "    mth_diff_series = pd.Series(mth_diff_list)\n",
    "    # print(mth_diff_list)\n",
    "    card_large_loan_cnt_m6 = (mth_diff_series<=6).sum()\n",
    "    card_large_loan_cnt_m12 = (mth_diff_series<=12).sum()\n",
    "    card_large_loan_cnt_m24 = (mth_diff_series<=24).sum()\n",
    "    # case 3 卡销信息\n",
    "    # x = \"\"\"\n",
    "    # 1卡销额6000累逾1+1/53/19.5\n",
    "    # 1卡销额24000累逾11+2/24999/21.3\n",
    "    # 1卡销额7万近24.1+1/1143/20.8\n",
    "    # 1卡销额7万近12.1+1/1143/20.8\n",
    "    # \"\"\"\n",
    "    # 授信额度/逾期情况/最高逾期金额/销户日期\n",
    "    pattern_kx_1 = re.compile(r\"(\\d+)卡销\")\n",
    "    result_kx_1  = pattern_kx_1.findall(x)\n",
    "    card_kx_cnt = len(result_kx_1)\n",
    "\n",
    "    pattern_kx = re.compile(r'(\\d+)(卡销)(额)(\\d+)(万*)'\n",
    "                            r'([累逾]*)(\\d*)(\\+*)(\\d*)'\n",
    "                            r'([近24]*|[近12]*)(\\.*)(\\d*)(\\+*)(\\d*)'\n",
    "                            r'/(\\d+)(万*)/([\\.\\d]{0,5})')\n",
    "    result_kx = pattern_kx.findall(x)\n",
    "    # ('1','卡销','额','6000','','累逾','1','+','1','','','','','53','','19.5')\n",
    "\n",
    "\n",
    "    card_kx_edu_list = [0]\n",
    "    card_kx_overed_flag = 0  # 卡销是否存在逾期\n",
    "    card_kx_overed_amt_max_list  = [0] # 最高逾期金额列表\n",
    "\n",
    "    for i in result_kx:\n",
    "        edu = 0\n",
    "        if i[3]!=\"\":\n",
    "            edu = float(i[3])\n",
    "            if i[4]!=\"\":\n",
    "                edu *= 1e4\n",
    "        card_kx_edu_list.append(edu)\n",
    "        # 是否存在逾期信息\n",
    "        if i[5]!=\"\" or i[9]!=\"\":\n",
    "            card_kx_overed_flag = 1\n",
    "        # 逾期金额\n",
    "        overed_amt = 0\n",
    "        if i[14] != \"\":\n",
    "            overed_amt = float(i[14])\n",
    "            if i[15]!=\"\":\n",
    "                overed_amt *= 1e4\n",
    "        card_kx_overed_amt_max_list.append(overed_amt)\n",
    "\n",
    "    card_kx_card_edu_max = max(card_kx_edu_list)\n",
    "    card_kx_overed_amt_max = max(card_kx_overed_amt_max_list)\n",
    "    card_kx_overed_amt_sum = sum(card_kx_overed_amt_max_list)\n",
    "\n",
    "    card_all_cnt = card_cnt + card_large_edu_cnt +card_kx_cnt\n",
    "    return card_cnt,card_edu_max,card_edu_mean,card_edu_used_sum_ratio,card_large_edu_cnt,card_large_edu_mth_repay_sum,card_large_edu_used_sum_ratio,\\\n",
    "        card_kx_cnt,card_kx_overed_flag,card_kx_overed_amt_max,card_kx_overed_amt_sum,card_all_cnt,card_large_loan_cnt_m6,card_large_loan_cnt_m12,card_large_loan_cnt_m24\n",
    "    # 总计信用卡额度\n",
    "\n",
    "def get_ks_card_edu_used_ratio(x):\n",
    "    \"\"\"\n",
    "    额度使用率： 授信总额/平均6个月使用额度\n",
    "    :param x:\n",
    "    eg.x = \"卡授254400/208465  卡授2万已用2万 卡授2万已用0\"\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    card_edu_used_ratio = 0\n",
    "    pattern_ks = re.compile(r'(卡授)(\\d+)(万*)([/|已用]*)(\\d+)(万*)')\n",
    "    result_ks = pattern_ks.findall(x)\n",
    "    edu_list = []\n",
    "    edu_used_list = []\n",
    "    for i in result_ks:\n",
    "        edu = np.float(i[1])\n",
    "        if i[2]!=\"\":\n",
    "            edu *= 1e4\n",
    "        edu_used = np.float(i[4])\n",
    "        if i[5] !=\"\":\n",
    "            edu_used *= 1e4\n",
    "        edu_list.append(edu)\n",
    "        edu_used_list.append(edu_used)\n",
    "\n",
    "    card_edu_sum_ks = np.sum(edu_list)\n",
    "    edu_used_sum_ks = np.sum(edu_used_list)\n",
    "    edu_used_ratio_ks = edu_used_sum_ks/card_edu_sum_ks\n",
    "    return  card_edu_sum_ks,edu_used_sum_ks,edu_used_ratio_ks"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
